package com.orchid.community.models.controller;


import com.orchid.community.common.utils.CodeEnum;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.DTO.ChannelListRequestDTO;
import com.orchid.community.models.DTO.ChannelRequestDTO;
import com.orchid.community.models.service.SceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/channel")
public class SceneController {

    @Autowired
    private SceneService sceneService;


    @PostMapping("/create")
    public MsgResponse createChannel(@RequestBody ChannelRequestDTO requestDTO) {
        if(StringUtils.isEmpty(requestDTO.getChannelNo())
                || StringUtils.isEmpty(requestDTO.getCity())
                || StringUtils.isEmpty(requestDTO.getContact())
                || StringUtils.isEmpty(requestDTO.getContactName())
                || StringUtils.isEmpty(requestDTO.getGender())
        ){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        return sceneService.createChannel(requestDTO);
    }

    @PostMapping("/delete")
    public MsgResponse deleteChannel(@RequestBody Map<String, String> map) {
        if(StringUtils.isEmpty(map.get("id"))){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        int result = sceneService.deleteChannel(map);
        if(result > 0){
            return ResultUtils.success("OK");
        }else {
            return ResultUtils.fail(CodeEnum.DEL_FAIL);
        }
    }

    /**
     * 修改
     * 修改渠道
     * @param requestDTO
     * @return
     */
    @PostMapping("/update")
    public MsgResponse updateChannel(@RequestBody ChannelRequestDTO requestDTO) {
        if(StringUtils.isEmpty(requestDTO.getChannelNo())
                || StringUtils.isEmpty(requestDTO.getCity())
                || StringUtils.isEmpty(requestDTO.getContact())
                || StringUtils.isEmpty(requestDTO.getContactName())
                || StringUtils.isEmpty(requestDTO.getGender())
        ){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        return sceneService.updateChannel(requestDTO);
    }

    /**
     * 分页查看渠道列表
     * @param requestDTO
     * @return
     */
    @PostMapping("/getChannelList")
    public MsgResponse getChannelList(@RequestBody ChannelListRequestDTO requestDTO) {
        return sceneService.getChannelList(requestDTO);

    }

    /**
     * 获取全部城市列表
     * @return
     */
    @PostMapping("/getAllCityList")
    public MsgResponse getAllCityList() {
        return sceneService.getAllCityList();
    }


    @PostMapping("/getAllChannel")
    public MsgResponse getAllChannel(){

        return sceneService.getAllChannel();
    }
}
